package lu.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MenuClassList extends ListView {

	private static final String KEY_ITEM = "menuItem";
	private static final String KEY_NUM = "classTotal";
	private static final String KEY_CHECK = "checked";

	private static final Map<String, Integer> icons = new HashMap<String, Integer>();
	static {
		icons.put("ic_best", R.drawable.ic_launcher);
	}

	private Context context;
	private List<Map<String, Object>> listMenuClasses = new ArrayList<Map<String, Object>>();
	private MenuItemInfo currentMenuClass = null;
	private MyAdapter adapter;

	public MenuClassList(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public MenuClassList(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public MenuClassList(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	private void init() {
		setAdapter(adapter = new MyAdapter(context));
		setScrollbarFadingEnabled(true);
		setCacheColorHint(0);
		setFadingEdgeLength(0);
		setDividerHeight(0);
		// setOnItemClickListener(new OnItemClickListener() {
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// Toast.makeText(getContext(), "" + position,
		// Toast.LENGTH_SHORT).show();
		// MenuClassList.this.checked(position);
		// }
		// });
	}

	public void setMenuClassList(List<MenuItemInfo> menuClassList) {
		if (menuClassList == null)
			return;
		listMenuClasses.clear();
		for (MenuItemInfo menuClass : menuClassList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(KEY_ITEM, menuClass);
			map.put(KEY_NUM, 0);
			map.put(KEY_CHECK, false);
			listMenuClasses.add(map);
		}
		adapter.notifyDataSetChanged();
	}

	public void deselect() {
		for (Map<String, Object> map : listMenuClasses) {
			map.put(KEY_CHECK, false);
		}
		adapter.notifyDataSetChanged();
	}

	public void refreshMenuClassNum() {
		adapter.notifyDataSetChanged();
	}

	public void checked(int index) {
		currentMenuClass = null;
		for (Map<String, Object> map : listMenuClasses)
			map.put(KEY_CHECK, false);
		Map<String, Object> map = listMenuClasses.get(index);
		if (map != null) {
			map.put(KEY_CHECK, true);
			currentMenuClass = (MenuItemInfo) map.get(KEY_ITEM);
		}
		adapter.notifyDataSetChanged();
	}

	public void addOne(int index) {
		listMenuClasses.get(index).put(KEY_NUM,
				(Integer) listMenuClasses.get(index).get(KEY_NUM) + 1);
		adapter.notifyDataSetChanged();
	}

	public void removeOne(int index) {
		listMenuClasses.get(index).put(KEY_NUM,
				(Integer) listMenuClasses.get(index).get(KEY_NUM) - 1);
		adapter.notifyDataSetChanged();
	}

	public MenuItemInfo getCurrentMenuClass() {
		return currentMenuClass;
	}

	public final class MenuClassViewHolder {
		public int position;
		public CheckedTextView checkedText;
		public TextView textNum;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return listMenuClasses.size();
		}

		@Override
		public Object getItem(int index) {
			return listMenuClasses.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MenuClassViewHolder holder;
			if (convertView == null) {
				holder = new MenuClassViewHolder();
				convertView = mInflater.inflate(R.layout.menu_class_item, null);
				holder.checkedText = (CheckedTextView) convertView
						.findViewById(R.id.checkedTextViewMenuClass);
				holder.textNum = (TextView) convertView
						.findViewById(R.id.textViewMenuClassNum);
				// Listener
				holder.checkedText.setTag(holder);
				holder.checkedText.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						int p = ((MenuClassViewHolder) v.getTag()).position;
						Toast.makeText(getContext(), "" + p, Toast.LENGTH_SHORT)
								.show();
						MenuClassList.this.checked(p);
					}
				});

				convertView.setTag(holder);
			} else {
				holder = (MenuClassViewHolder) convertView.getTag();
			}

			// Attributes 注意，对�?��控件的赋值操作过程中，if else的每个分支都要有具体赋�?语句
			MenuItemInfo menuItem = (MenuItemInfo) listMenuClasses
					.get(position).get(KEY_ITEM);
			holder.position = position;
			holder.checkedText.setText(menuItem.getName());
			Integer iconID = icons.get(menuItem.getIcon());
			Drawable draw;
			if (iconID != null)
				draw = MenuClassList.this.context.getResources().getDrawable(
						iconID.intValue());
			else
				draw = MenuClassList.this.context.getResources().getDrawable(
						R.drawable.ic_launcher);
			draw.setBounds(0, 0, 38, 22);// TODO 这个大小是针�?60dpi�?
			holder.checkedText.setCompoundDrawables(draw, null, null, null);
			holder.checkedText.setChecked((Boolean) listMenuClasses.get(
					position).get(KEY_CHECK));
			// holder.checkedText
			// .setBackgroundResource((Boolean) listMenuClasses.get(
			// position).get(KEY_CHECK) ?
			// R.drawable.btn_menu_class_pressed_yellow
			// : R.drawable.btn_menu_class_normal);
			Integer num = (Integer) listMenuClasses.get(position).get(KEY_NUM);
			holder.textNum.setText(num.toString());
			if (num > 0)
				holder.textNum.setVisibility(VISIBLE);
			else
				holder.textNum.setVisibility(INVISIBLE);

			return convertView;
		}
	}
}
